import numpy as np
5 Numpy
=np.array([np.arange(6)]*6)+np.arange(0,51,10).reshape(6,1)
a a
array([[ 0, 1, 2, 3, 4, 5],
[10, 11, 12, 13, 14, 15],
[20, 21, 22, 23, 24, 25],
[30, 31, 32, 33, 34, 35],
[40, 41, 42, 43, 44, 45],
[50, 51, 52, 53, 54, 55]])
0,3:5] a[
array([3, 4])
4:,4:] a[
array([[44, 45],
[54, 55]])
0:,2] a[
array([ 2, 12, 22, 32, 42, 52])
0:,2:3] a[
array([[ 2],
[12],
[22],
[32],
[42],
[52]])
0:,2]=[1,2,3,4,5,6]
a[ a
array([[ 0, 1, 1, 3, 4, 5],
[10, 11, 2, 13, 14, 15],
[20, 21, 3, 23, 24, 25],
[30, 31, 4, 33, 34, 35],
[40, 41, 5, 43, 44, 45],
[50, 51, 6, 53, 54, 55]])
2::2,0:6:2] a[
array([[20, 3, 24],
[40, 5, 44]])
# Operations on Arrays
# Numpy aggregates
# Exercise 4
# F=9/5C+32
=np.arange(20,31)
c*9/5+32 c
array([68. , 69.8, 71.6, 73.4, 75.2, 77. , 78.8, 80.6, 82.4, 84.2, 86. ])
# Exercise 5
1)
np.random.seed(=np.random.rand(5)
Zprint(Z,(Z-Z.min())/(Z.max()-Z.min()),sep="\n")
[4.17022005e-01 7.20324493e-01 1.14374817e-04 3.02332573e-01
1.46755891e-01]
[0.57886944 1. 0. 0.41962504 0.20360935]
# Exercise 6
=np.random.uniform(0,10,10)
Z np.floor(Z)
array([6., 4., 5., 1., 1., 8., 9., 3., 6., 8.])
-1 np.ceil(Z)
array([6., 4., 5., 1., 1., 8., 9., 3., 6., 8.])
-Z%1 Z
array([6., 4., 5., 1., 1., 8., 9., 3., 6., 8.])
# Exercise 7
=np.zeros((6,6))
x=np.arange(0,6) y
+y x
array([[0., 1., 2., 3., 4., 5.],
[0., 1., 2., 3., 4., 5.],
[0., 1., 2., 3., 4., 5.],
[0., 1., 2., 3., 4., 5.],
[0., 1., 2., 3., 4., 5.],
[0., 1., 2., 3., 4., 5.]])
+y[:,np.newaxis] x
array([[0., 0., 0., 0., 0., 0.],
[1., 1., 1., 1., 1., 1.],
[2., 2., 2., 2., 2., 2.],
[3., 3., 3., 3., 3., 3.],
[4., 4., 4., 4., 4., 4.],
[5., 5., 5., 5., 5., 5.]])
+y.reshape(6,1) x
array([[0., 0., 0., 0., 0., 0.],
[1., 1., 1., 1., 1., 1.],
[2., 2., 2., 2., 2., 2.],
[3., 3., 3., 3., 3., 3.],
[4., 4., 4., 4., 4., 4.],
[5., 5., 5., 5., 5., 5.]])
# Exercise 9
123)
np.random.seed(= np.random.randn(6, 4); mat mat
array([[-1.0856306 , 0.99734545, 0.2829785 , -1.50629471],
[-0.57860025, 1.65143654, -2.42667924, -0.42891263],
[ 1.26593626, -0.8667404 , -0.67888615, -0.09470897],
[ 1.49138963, -0.638902 , -0.44398196, -0.43435128],
[ 2.20593008, 2.18678609, 1.0040539 , 0.3861864 ],
[ 0.73736858, 1.49073203, -0.93583387, 1.17582904]])
=np.concatenate([mat,np.exp(mat[:,0])[:,np.newaxis]],axis=1)
mat mat
array([[-1.0856306 , 0.99734545, 0.2829785 , -1.50629471, 0.33768877],
[-0.57860025, 1.65143654, -2.42667924, -0.42891263, 0.56068263],
[ 1.26593626, -0.8667404 , -0.67888615, -0.09470897, 3.54641154],
[ 1.49138963, -0.638902 , -0.44398196, -0.43435128, 4.44326571],
[ 2.20593008, 2.18678609, 1.0040539 , 0.3861864 , 9.07869158],
[ 0.73736858, 1.49073203, -0.93583387, 1.17582904, 2.09042747]])
=mat+[1,-1,1,-1,1]; mat mat
array([[-8.56306033e-02, -2.65455342e-03, 1.28297850e+00,
-2.50629471e+00, 1.33768877e+00],
[ 4.21399748e-01, 6.51436537e-01, -1.42667924e+00,
-1.42891263e+00, 1.56068263e+00],
[ 2.26593626e+00, -1.86674040e+00, 3.21113848e-01,
-1.09470897e+00, 4.54641154e+00],
[ 2.49138963e+00, -1.63890200e+00, 5.56018040e-01,
-1.43435128e+00, 5.44326571e+00],
[ 3.20593008e+00, 1.18678609e+00, 2.00405390e+00,
-6.13813601e-01, 1.00786916e+01],
[ 1.73736858e+00, 4.90732028e-01, 6.41661316e-02,
1.75829045e-01, 3.09042747e+00]])
=mat*np.array([1/10,1/10,1/10,10,10,10]).reshape(6,1)
mat mat
array([[-8.56306033e-03, -2.65455342e-04, 1.28297850e-01,
-2.50629471e-01, 1.33768877e-01],
[ 4.21399748e-02, 6.51436537e-02, -1.42667924e-01,
-1.42891263e-01, 1.56068263e-01],
[ 2.26593626e-01, -1.86674040e-01, 3.21113848e-02,
-1.09470897e-01, 4.54641154e-01],
[ 2.49138963e+01, -1.63890200e+01, 5.56018040e+00,
-1.43435128e+01, 5.44326571e+01],
[ 3.20593008e+01, 1.18678609e+01, 2.00405390e+01,
-6.13813601e+00, 1.00786916e+02],
[ 1.73736858e+01, 4.90732028e+00, 6.41661316e-01,
1.75829045e+00, 3.09042747e+01]])
=0) np.median(mat,axis
array([ 8.80013969, 0.0324391 , 0.38497958, -0.19676037, 15.67945792])
70,axis=0) np.percentile(mat,
array([21.14379101, 2.48623197, 3.10092086, -0.12618108, 42.66846589])
2,:])[::-1] np.sort(mat[
array([ 0.45464115, 0.22659363, 0.03211138, -0.1094709 , -0.18667404])
2,:])[::-1] np.argsort(mat[
array([4, 0, 2, 3, 1])
sum(np.where(np.exp(mat)>3,1,0),axis=1) np.
array([0, 0, 0, 3, 4, 4])
# or
sum(np.exp(mat)>3,axis=1) np.
array([0, 0, 0, 3, 4, 4])
>np.mean(mat,axis=0)) np.nonzero(mat
(array([0, 1, 1, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5]),
array([3, 1, 3, 3, 0, 2, 4, 0, 1, 2, 4, 0, 1, 3]))